package best.driver.android.Activity.Finance.Adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import best.driver.android.Activity.Finance.TradeFeeActivity;
import best.driver.android.Beans.CarCheckInfo;
import best.driver.android.Beans.CarItemInfo;
import best.driver.android.Beans.TradeItemInfo;
import best.driver.android.Beans.TradeType;
import best.driver.android.Event.SelectCarEvent;
import best.driver.android.R;
import best.driver.android.Utils.TimeUtil;
import best.driver.android.Utils.Util;
import best.driver.android.Utils.Utils;
import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;

import java.util.ArrayList;
import java.util.List;

public class TradeInfoAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    private Context mContext;

    private List<TradeItemInfo> mTradeItems = new ArrayList<TradeItemInfo>();

    public TradeInfoAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<TradeItemInfo> list) {
        mTradeItems.clear();
        if(list == null || list.size() == 0) {
            return;
        }
        mTradeItems.addAll(list);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mTradeItems.size();
    }

    @Override
    public TradeItemInfo getItem(int position) {
        return mTradeItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = mInflater.inflate(R.layout.item_trade_info, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, TradeFeeActivity.class);
                intent.putExtra("tradeInfo", getItem(position));
                mContext.startActivity(intent);
            }
        });
        setConvertView(holder, position);
        return convertView;
    }

    private void setConvertView(ViewHolder holder, int position) {
        TradeItemInfo item = getItem(position);
        holder.mTvTitle.setText(TextUtils.isEmpty(TradeType.getInstance().getPayTypeMap().get(item.tradeType)) ?
                "" : TradeType.getInstance().getPayTypeMap().get(item.tradeType));
        holder.mTvDesc.setText(TextUtils.isEmpty(item.memo) ? "" : item.memo);
        holder.mTvTime.setText(TimeUtil.convertToYMDHM(item.tradeTime));
        boolean isPositive = (item.amount > 0) ? true : false;
        String money = Util.getOnePointNumber(Math.abs(item.amount));
        holder.mTvMoney.setText((isPositive ? "+" : "-") + money);
    }

    static class ViewHolder {

        @InjectView(R.id.tv_title)
        TextView mTvTitle;

        @InjectView(R.id.tv_money)
        TextView mTvMoney;

        @InjectView(R.id.tv_time)
        TextView mTvTime;

        @InjectView(R.id.tv_desc)
        TextView mTvDesc;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
